package model;

import java.util.Collection;
import data.Project;

public class SimilarityFunction {
	int num_params = 16;
	
	double[] msum = new double[num_params]; 

	
	private double distance(Project proj1, Project proj2){
		
		double sum = 0;
		for(int i=0; i<num_params; i++){
			String tmp = Pharser.fnames[i];
			if(proj1.params.get(tmp)!=null){
				double p1 = proj1.params.get(tmp).dvalue;
				double p2 = proj2.params.get(tmp).dvalue;
				sum += Math.abs(p1-p2)/msum[i] * Math.abs(p1-p2)/msum[i];
			}else{
			}
		}
		return Math.sqrt((sum)/proj1.params.size());
		
	}
	
	
		
	public void initMinMax(Collection<Project> proj) {

		double max = 0;
		double min = 0;
	
	
		 
		for (int i = 0; i < num_params; i++) {
			String tmp = Pharser.fnames[i];
			for (Project p: proj) {
				double value = p.params.get(tmp).dvalue;
				if (value > 0){
					if (value > max) {
						max = value;
					}
					if (value < min) {
						min = value;
	
					}	
					
				}
			
			}
			msum[i] = Math.abs(max-min);
		}

	}
	public double similarityFunction(Project proj1, Project proj2) {
		return	1-distance(proj1, proj2);
		
	}
}
